﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;


namespace LibreriaDeClases
{
    class GestorDeConexion
    {
    private SqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;

    //Constructor
    public GestorDeConexion()
    {
        Initialize();
    }

    //Initialize values
    private void Initialize()
    {
        string connectionString = "Data Source=HORACLES7-PC;Initial Catalog=REPARACIONES_DB;Integrated Security=True";

        connection = new SqlConnection(connectionString);
        
    }

    //open connection to database
    private bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (SqlException ex)
        {
            //When handling errors, you can your application's response based 
            //on the error number.
            //The two most common error numbers when connecting are as follows:
            //0: Cannot connect to server.
            //1045: Invalid user name and/or password.
            switch (ex.Number)
            {
                case 0:
                    Console.WriteLine("Cannot connect to server.  Contact administrator");
                    break;

                case 1045:
                    Console.WriteLine("Invalid username/password, please try again");
                    break;
            }
            return false;
        }
    }

    //Close connection
    public bool CloseConnection()
    {
        try
        {
            connection.Close();
            return true;
        }
        catch (SqlException ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }
    }

    
    //No olvidar cerrar la conexion despues de usarla.
    public SqlConnection getConexion()
    {
        OpenConnection();
        return connection;        
    }
    }
}
